import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

/**
 * Generated class for the DemoListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-demo-list',
  templateUrl: 'demo-list.html',
})
export class DemoListPage {
  listData = [];
  constructor(public navCtrl: NavController, public navParams: NavParams) {
    for (let i = 0; i <= 15; i++) {
      var tempjson = {
        id: i,
        title: 'new title ' + (i + 1),
        subtitle: 'subtitl aaaaaaa bbbbbb',
        form_Data: {
          name: "name_" + i,
          age: "0",
          sex: "0",
          property_1: "pro_1_val_" + i,
          property_2: "pro_2_val_" + i,
          property_3: "pro_3_val_" + i,
        }
      };
      this.listData.push(tempjson);
    }
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad DemoListPage');
  }

  //明细页面回调承诺
  //注意此处只能用箭头函数来编写，箭头函数内的this值继承自外围作用域
  recal_promise = (state: any, bakparam: any, idx: any) => {
    return new Promise((reslove, reject) => {
      if (state == "saveok") {
        this.listData[idx] = bakparam;
        reslove('ok');
      } else {
        reject(Error('erro'));
      }
    })
  }
  // 错误示范
  // recal_promise(state: any,bakparam : any,idx:any){
  //   return new Promise((reslove,reject)=>{
  //     if(state=="saveok"){
  //       //此处的this作用域会出现错误
  //       this.listData[idx] = bakparam
  //       reslove('ok')
  //     }else{
  //       reject(Error('erro'))
  //     }
  //   })
  // }



  show_detail(data, idx) {
    //  alert(JSON.stringify(data))
    let param = { index: idx, param: data, reCallback: this.recal_promise };
    this.navCtrl.push('DemoDetailPage', param);
  }

}
